/**
 * 季度时间 [begin,end) 开始时间包含, 结束时间不包含
 * 作者：致心 vue项目所有文件命名全部采用中划线分隔   utils
 * 创建时间：2020/12/8 15:10
 */
import typeOfUtils from '@/utils/type-of-utils'

const STATUS = {
  FIRSTQUARTER: {
    time: {
      begin: '1-1',
      end: '4-1'
    },
    value: 1
  },
  SECONDQUARTER: {
    time: {
      begin: '4-1',
      end: '7-1'
    },
    value: 2
  },
  THIRDQUATER: {
    time: {
      begin: '7-1',
      end: '10-1'
    },
    value: 3
  },
  FOURTHQUARTER: {
    time: {
      begin: '10-1',
      end: '1-1'
    },
    value: 4
  }
}

const oneDay = 24 * 60 * 60 * 1000

/**
 * @Description :根据时间获得当前的季度,如果传入的是季度的最后一天,则获取到下一个季度
 * @Author :致心
 * @Date :2020/12/8 15:17
 */
/**
 *
 * @param date {Date}
 * @return {number}
 */
function getQuarter(date) {
  const month = date.getMonth() + 1
  let date1 = date.getDate()
  const find = Object.values(STATUS).find(value => {
    if (`${month}-${date1}` === value.time.end) { // 如果是季度的最后一天那么就加一天
      date1 = parseDate(date.getTime() + 1000 * 60 * 60 * 24).getDate()
    }
    return isDuringDate(`${month}-${date1}`, value.time.begin, value.time.end)
  })
  if (find) {
    return find.value
  }
  return 0
}

/**
 * @Description : 是否在在时间段里面
 * @Author :致心
 * @Date :2020/12/8 15:43
 * @param curDateStr {string}
 * @param beginDateStr {string}
 * @param endDateStr {string}
 * @returns {boolean|boolean}
 */
function isDuringDate(curDateStr, beginDateStr, endDateStr) {
  const data1 = curDateStr.split('-')
  curDateStr = '2000-' + curDateStr
  const data2 = beginDateStr.split('-')
  const data3 = endDateStr.split('-')
  if (Number(data2[0]) > Number(data1[0])) {
    beginDateStr = '1999-' + beginDateStr
  } else {
    beginDateStr = '2000-' + beginDateStr
  }
  if (Number(data2[0]) > Number(data3[0])) {
    endDateStr = '2001-' + endDateStr
  } else {
    endDateStr = '2000-' + endDateStr
  }
  const curDate = parseDate(curDateStr).getTime()
  const beginDate = parseDate(beginDateStr).getTime()
  const endDate = parseDate(endDateStr).getTime()
  return curDate >= beginDate && curDate <= endDate
}

/**
 * @Description :根据那一年和那个季度获取下一个季度的开始时间和结束时间
 * @Author :致心
 * @Date :2020/12/8 17:01
 * @param year {string|number}
 * @param quarter {number}
 * @returns {{end: string, begin: string}}
 */
function getTheNextQuarter(year, quarter) {
  quarter++
  if (quarter === Object.values(STATUS).length + 1) {
    quarter = 1
  }
  const find = Object.values(STATUS).find(value => {
    return value.value === quarter
  })
  const time = {
    begin: '',
    end: ''
  }
  if (find) {
    const { begin, end } = find.time
    // 判断季度是不是跨年
    if (parseDate(`${year}-${begin}`) > parseDate(`${year}-${end}`)) { // 跨年
      time.begin = `${year}-${begin}`
      time.end = `${Number(year) + 1}-${end}`
    } else {
      time.begin = `${year}-${begin}`
      time.end = `${year}-${end}`
    }
  }
  return time
}

/**
 * 补零函数
 * @param zero
 * @returns {string}
 */
function doHandleZero(zero) {
  let date = zero
  if (zero.toString().length === 1) {
    date = '0' + zero
  }
  return date
}

/**
 * 获取月份第一天 如果dateStr为null就是当月时间
 * @param dateStr {String}
 * @param toDate {Boolean}
 * @returns {string|Date}
 */
function getMonthFirstDay(dateStr, toDate = false) {
  let date
  if (typeOfUtils.isEmpty(dateStr)) {
    date = parseDate()
  } else {
    date = parseDate(dateStr)
  }
  const tYear = date.getFullYear()
  let tMonth = date.getMonth()
  tMonth = doHandleZero(tMonth + 1)
  if (toDate) {
    return parseDate(tYear + '-' + tMonth + '-01')
  }
  return tYear + '-' + tMonth + '-01'
}

/**
 * 根据开始月份和结束月份返回所有的月份
 * @param monthBegin
 * @param monthEnd
 * @return {string[]|[]}
 */
function getMouthLimit(monthBegin = '2021-07', monthEnd = '2021-07') {
  if (typeOfUtils.isEmpty(monthBegin)) {
    throw new Error('开始月份不能为空')
  }
  if (typeOfUtils.isEmpty(monthEnd)) {
    throw new Error('结束月份不能为空')
  }
  if (monthBegin === monthEnd) {
    return [monthBegin]
  }
  if (parseDate(monthEnd) < parseDate(monthBegin)) {
    throw new Error('时间区间传入错误')
  }
  const result = []
  const beginYear = parseDate(monthBegin).getFullYear()
  const beginMonth = parseDate(monthBegin).getMonth() + 1
  const endYear = parseDate(monthEnd).getFullYear()
  const endMonth = parseDate(monthEnd).getMonth() + 1
  for (let i = beginYear; i <= endYear; i++) {
    for (let j = (i === beginYear ? beginMonth : 1); j <= (i === endYear ? endMonth : 12); j++) {
      result.push(i + '-' + (j > 9 ? j : '0' + j))
    }
  }
  return result
}

/**
 * 判断是不是闰年
 * @param year
 * @return {boolean|boolean}
 */
function isLeapYear(year) {
  return (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0)
}

/**
 * 获取月份有多少天
 * @param time
 * @return {number}
 */
function getMonthDays(time = '') {
  if (typeOfUtils.isEmpty(time)) {
    time = parseDate()
  }
  time = parseDate(time)
  const month = time.getMonth() + 1
  const year = time.getFullYear()
  switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31
    case 4:
    case 6:
    case 9:
    case 11:
      return 30
    case 2:
      return isLeapYear(year) ? 29 : 28
  }
}

/**
 * 获取上一天
 * @param day
 * @return {string}
 */
function getPerDay(day = '2021-09-09') {
  let result = '2021-09-08'
  let lastDay = parseDate(day).getTime() - oneDay
  lastDay = parseDate(lastDay)
  result = `${lastDay.getFullYear()}-${doHandleZero(lastDay.getMonth() + 1)}-${doHandleZero(lastDay.getDate())}`
  return result
}

/**
 * 返回Date 把- 替换成 /
 * @param time
 * @return {Date}
 */
function parseDate(time) {
  if (typeOfUtils.isEmpty(time)) {
    return new Date()
  } else {
    if (typeof time === 'string' && time.indexOf('-')) {
      const reg = new RegExp('-', 'gi')
      time = time.replace(reg, '/')
    }
    if (/^[0-9]+$/.test(time)) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    return new Date(time)
  }
}

// 获取出生年龄，周岁、月、天、时、分、秒
function getGrowAge(birthday = '1998-10-01 02:02:02') {
  const now = new Date()
  let year = now.getFullYear()
  let month = now.getMonth() + 1
  let day = now.getDate()
  let hour = now.getHours()
  let minute = now.getMinutes()
  const second = now.getSeconds()

  const myDate = parseDate(birthday)
  const myYear = myDate.getFullYear()
  const myMonth = myDate.getMonth() + 1
  const myDay = myDate.getDate()
  const myHour = myDate.getHours()
  const myMinute = myDate.getMinutes()
  const mySecond = myDate.getSeconds()

  let gapSecond = second - mySecond
  if (gapSecond < 0) {
    minute -= 1
    gapSecond = 60 - mySecond + second
  }
  // gapSecond = gapSecond<10? ("0"+gapSecond): gapSecond;
  let gapMinute = minute - myMinute
  if (gapMinute < 0) {
    hour -= 1
    gapMinute = 60 - myMinute + minute
  }
  // gapMinute = gapMinute<10? ("0"+gapMinute): gapMinute;
  let gapHour = hour - myHour
  if (gapHour < 0) {
    day -= 1
    gapHour = 24 - myHour + hour
  }
  // gapHour = gapHour<10? ("0"+gapHour): gapHour;
  let gapDay = day - myDay
  if (gapDay < 0) {
    month -= 1
    gapDay = getMonthDays(birthday) - myDay + day
  }
  // gapDay = gapDay<10? ("0"+gapDay): gapDay;
  let gapMonth = month - myMonth
  if (gapMonth < 0) {
    year -= 1
    gapMonth = 12 - myMonth + month
  }
  // gapMonth = gapMonth<10? ("0"+gapMonth): gapMonth;
  let gapYear = year - myYear
  if (gapYear < 0) {
    gapYear = 0
  }

  return (gapYear > 0 ? gapYear + '岁 ' : '') +
    (gapMonth > 0 ? gapMonth + '月 ' : '') +
    (gapDay > 0 ? gapDay + '天 ' : '') +
    (gapHour > 0 ? gapHour + '时 ' : '') +
    (gapMinute > 0 ? gapMinute + '分 ' : '') +
    (gapSecond > 0 ? gapSecond + '秒 ' : '')
}

/**
 * 计算月份
 * @param time {string|Date|Number}
 * @param count {Number} 计算要相差的月份
 * @return {string}
 */
function getMonth(time = new Date(), count = 0) { // 获取月份
  count = Number(count)
  time = parseDate(time)
  let month = time.getMonth() + count + 1
  let year = time.getFullYear()
  if (month < 1) {
    year--
    month += 12
  }
  if (month > 12) {
    year++
    month -= 12
  }
  return `${year}-${doHandleZero(month)}`
}

const timeTool = {
  STATUS, // 定义季度
  getMouthLimit, // 根据开始月份和结束月份返回所有的月份
  getQuarter, // 根据时间获得当前的季度,如果传入的是季度的最后一天,则获取到下一个季度
  getTheNextQuarter, // 根据那一年和那个季度获取下一个季度的开始时间和结束时间
  getMonthFirstDay, // 获取月份第一天
  getMonthDays, // 获取月份有多少天
  parseDate, // 返回Date 把- 替换成 /
  getGrowAge, // 获取出生年龄，周岁、月、天、时、分、秒
  getMonth// 获取月份
}

export default timeTool
